<?php
class Ttnavigation {
	protected $total, $limit, $current;
	
	const MAX_RANGE = 5;
	
	function __construct($number_of_records = 0, $limit = 5) {
		$this->total = $number_of_records;
		$this->limit = $limit;
	}
	
	public function set_current($current) {
		$this->current = $current >= 1 ? $current : 1;
	}
	
	public function get_current() {
		return $this->current;
	}
	
	public function get_first() {
		return 1;
	}
	
	public function get_last() {
		return ceil($this->total / $this->limit); 
	}
	
	public function get_next() {
		return min($this->current + 1, $this->get_last());
	}
	
	public function get_previous() {
		return max(1, $this->current - 1);
	}
	
	public function get_series() {
		$begin = (ceil($this->current / self::MAX_RANGE) - 1) * self::MAX_RANGE + 1;
		$end = min($begin + self::MAX_RANGE, $this->get_last());
		$series = array();
		for($i = $begin; $i <= $end; ++$i)
			$series[] = $i;
		return $series;
	}
	
	public function get_max_page() {
		return ceil($this->total / $this->limit);
	}
	
	public function get_html_navigation($link_format) {
		$result = '';
		$begin = (ceil($this->current / self::MAX_RANGE) - 1) * self::MAX_RANGE + 1;
		$end = min($begin + self::MAX_RANGE - 1, $this->get_last());
		
		if($this->current != $this->get_first()) {
			$result .= '<a href="' . $link_format . $this->get_first() . '">First' . '</a>'; 
			$result .= '<a href="' . $link_format . $this->get_previous() . '">Previous' . '</a>&nbsp;';
		}
		
		if($begin != $end) {	
			for($i = $begin; $i <= $end; ++$i) {
					if($i != $this->current)
						$result .= '<a href="' . $link_format . $i . '">' . $i . '</a>';
					else $result .= '<span>' . $i . '</span>';
			}
		}
		if($this->current != $this->get_last() && $this->get_last() != 0) {
			$result .= '&nbsp;<a href="' . $link_format . $this->get_next() . '">Next' . '</a>'; 
			$result .= '<a href="' . $link_format . $this->get_last() . '">Last' . '</a>';
		}
		return $result;
	}
}
?>